<template>
  <!-- 登录界面 -->
  <div class="formBox">
    <div class="login-form-wrap">
      <h2>登录诊所管理系统</h2>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        class="demo-ruleForm"
      >
        <el-form-item prop="name">
          <el-input v-model="ruleForm.name" placeholder="请输入用户名">
            <!-- <i slot="prefix" class="el-input__icon el-icon-search"></i> -->
            <i slot="prefix" class="iconi">
              <img :src="icon1" alt="" srcset="" />
            </i>
          </el-input>
        </el-form-item>
        <el-form-item prop="phone">
          <el-input v-model="ruleForm.phone" placeholder="请输入手机号码">
            <!-- <i slot="prefix" class="el-input__icon el-icon-search"></i> -->
            <i slot="prefix" class="iconi">
              <img :src="icon2" alt="" srcset="" />
            </i>
          </el-input>
        </el-form-item>
        <el-form-item prop="pwd">
          <el-input v-model="ruleForm.pwd" placeholder="请输入密码">
            <!-- <i slot="prefix" class="el-input__icon el-icon-search"></i> -->
            <i slot="prefix" class="iconi">
              <img :src="icon3" alt="" srcset="" />
            </i>
          </el-input>
        </el-form-item>
        <div style="overflow: hidden">
          <el-form-item prop="type" style="float: left">
            <el-checkbox-group v-model="ruleForm.type">
              <el-checkbox label="1个月内免登录" name="type"></el-checkbox>
            </el-checkbox-group>
          </el-form-item>
          <el-link :underline="false" style="float: right; line-height: 30px"
             class="forgetPwd" >忘记密码</el-link
          >
          <!-- @click='findpwd' -->
        </div>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >登录</el-button
          >
        </el-form-item>
        <el-link :underline="false" class="apply" @click="apply">申请试用</el-link>
      </el-form>
    </div>
  </div>
</template>

<script>
import { Login, IsExistUserName } from "../../../api/LoginApi.js";
import { setToken } from "../../../utils/cookieUtil.js";

var icon1 = require("../../../assect/loginimgs/main-icon-01.png");
var icon2 = require("../../../assect/loginimgs/main-icon-02.png");
var icon3 = require("../../../assect/loginimgs/main-icon-03.png");
export default {
  data() {
    var checkName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("名字不能为空"));
      }
      setTimeout(() => {
        IsExistUserName({ name: value }).then((res) => {
          if (res.code == 200) {
            callback();
          } else {
            return callback(new Error(res.msg));
          }
        });
      }, 200);
    };
    var checkPhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("手机号不能为空"));
      }
      setTimeout(() => {
        callback();
      }, 200);
    };
    var checkPwd = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("密码不能为空"));
      }
      setTimeout(() => {
        callback();
      }, 200);
    };
    return {
      icon1,
      icon2,
      icon3,
      ruleForm: {
        name: "",
        phone: "",
        pwd: "",
        type: "",
      },
      rules: {
        name: [{ validator: checkName, trigger: "blur" }],
        phone: [{ validator: checkPhone, trigger: "blur" }],
        pwd: [{ validator: checkPwd, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          Login(this.ruleForm).then((res) => {
            if (res.code == 200) {
              //存储cookie
              console.log("当前登录的token：", res.data);
              setToken("token", res.data);
              //跳转首页
              this.$router.push("/");
              this.$message({
                message: "登录成功",
                type: "success",
              });
            } else {
              this.$message({
                message: res.msg,
                type: "warning",
              });
            }
          });
        } else {
          this.$message({
            message: "请输入正确的信息",
            type: "warning",
          });
          return false;
        }
      });
    },
    // 跳转到忘记密码界面
    // findpwd(){
    //   this.$router.push("/login/findpwd");
    // },
    // 跳转到申请试用界面
    apply(){
      this.$router.push("/login/apply");
    }
  },
};
</script>
<style lang="less" scoped>
.formBox {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  margin: auto;
  border-radius: 20px;
  width: 1202px;
  height: 667px;
  background: url(../../../assect/loginimgs/main-chahua.png) no-repeat left
    center #fff;
}
.login-form-wrap {
  width: 400px;
  float: right;
  text-align: center;
  box-sizing: border-box;
  margin-top: 110px;
  margin-right: 120px;
  h2 {
    text-align: left;
    font-weight: bold;
    font-size: 28px;
    margin-bottom: 35px;
  }
  /deep/.el-form-item {
    margin-bottom: 24px;
  }
  /deep/.el-input__inner {
    background: #f1f1fd;
    border-radius: 10px;
    border: 0;
    height: 48px;
    padding-left:45px;
  }
  .iconi {
    height: 48px;
    line-height: 44px;
    img {
      vertical-align: middle;
      margin-right:5px;
    }
  }
  /deep/.el-button {
    width: 100%;
    height: 50px;
    margin-bottom: 15px;
    background-color: #666fe8;
    border: 0;
    border-radius: 8px;
  }
  .forgetPwd{
    color:#878CED;
  }
  /deep/.el-checkbox__input.is-checked .el-checkbox__inner, .el-checkbox__input.is-indeterminate .el-checkbox__inner{
    background-color:#666fe8;
    color:#666fe8;
  }
  .el-checkbox{margin-left:8px;}
  /deep/.el-checkbox__input.is-checked+.el-checkbox__label{
     color:#666fe8;
  }
}
</style>